<?php
session_start();

$id = $_REQUEST["id"];

$db = NEW myDBs();

//$db->selectDB("account");

if ($id) {
    $sql = "select * from tbl_petout where id = '$id'";

    $result = $db->runSQL($sql);
    $row = $db->fetch_object($result);

    $date_create = $row->date_create;

    $panngan2_id = $row->panngan2_id;
    $panngan3_id = $row->panngan3_id;

    $budget_year = $row->budget_year;

    $month_year = $row->month_year;
    $vender_id = $row->vender_id;

    $money_out2_id = $row->money_out2_id;
    $money_out3_id = $row->money_out3_id;

    $amount = $row->amount;
    $comments = $row->comments;
}
?>

<script type="text/javascript">
    $(document).ready(function() {

        $("#money_out2_id").select2({
            placeholder: "เลือก..."
                    //,allowClear: true
                    //, maximumSelectionSize: 2
                    //, tags:["red", "green", "blue"]
        });
        $("#money_out3_id").select2({
            placeholder: "เลือก..."
        });

        $("#panngan2_id").select2({
            placeholder: "เลือก..."
        });
        $("#panngan3_id").select2({
            placeholder: "เลือก..."
        });
        $("#vender").select2({
            placeholder: "เลือก..."
        });

//        $("#series").chained("#mark"); 
//         or $("#series").chainedTo("#mark"); 

        $("#lvl1_id").select2({
            placeholder: "เลือก..."
        });

        $("#panngan2_id").chained("#lvl1_id");
        $("#panngan3_id").chained("#panngan2_id");
        $("#money_out3_id").chained("#money_out2_id");

        $("#date_create").datepicker({
            dateFormat: "yy-mm-dd"
                    //defaultDate: new Date()
                    //showMonthAfterYear:true,

        });
        $("#month_year").datepicker({
            dateFormat: "yy-mm"
        });

        $("#money_out2_id").change(function() {
            var money2_id = $(this).val();

            if (money2_id == "230102") {
//                alert("sadfadsf");
                $.post('tax1.php', {tablename: 'expenses_lvl1', dd: 'ex1_id'}, function(data) {
                    //$("#expenses1").html(data);
                    $("#amount").attr("readonly", "true");
                    $("#amount").val(data);
                });

            } else {
                //$("#amount").attr("readonly", "false");
                $("#amount").removeAttr("readonly");
            }
        });

        $("#money_out3_id").change(function() {
            var money3_id = $(this).val();

            if (money3_id == "67") {
//                alert("sadfadsf");
                $.post('sso.php', {tablename: 'expenses_lvl1', dd: 'ex1_id'}, function(data) {
                    //$("#expenses1").html(data);
                    $("#amount").attr("readonly", "true");
                    $("#amount").val(data);
                });

            } else {
                //$("#amount").attr("readonly", "false");
                $("#amount").removeAttr("readonly");
            }
        });

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();

                $.ajax({
                    type: "POST",
                    url: "petout_process.php",
                    data: $('#petout').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {

//                        $("#test").html(data);
                        window.location.href = "index.php?pages=petout&pageid=9";

                    }
                });
            }
        });
    });

</script>

<div id='test'></div>
<form class="form-horizontal" id="petout" name="petout" method="post" action="index.php" novalidate="">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <input type="hidden" name="task" value="<?php echo ($id) ? "edit" : "new"; ?>">
    <div class="row">
        <div class="span12">
            <div class="panel panel-info">
                <div class="panel-heading">ฎีกาเงินนอกงบประมาณ</div>
                <div class="panel-body">
                    <div class='control-group'>
                        <label class="control-label" for="ปีงบประมาณ">ปีงบประมาณ</label>
                        <div class="controls ">
                            <select id="budget_year" name="budget_year" <?php echo ($id) ? "readonly" : ""; ?>>

                                <?php
                                $this_y = date("Y");

                                if ($this_y < 2556)
                                    $this_y += 543;

                                if (!$budget_year)
                                    $budget_year = $this_y;

                                for ($i = 2556; $i <= $this_y + 1; $i++) {
                                    if ($i == $budget_year) {
                                        $selected = "selected";
                                    } else {
                                        $selected = "";
                                    }
                                    echo "<option value='$i' $selected>" . $i . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class='control-group'>
                        <label class="control-label" for="วันที่">วันที่</label>
                        <div class="controls ">
                            <input type="text" id="date_create" name="date_create" value="<?php echo $date_create; ?>" />

                        </div>
                    </div>

                    <div class='control-group'>
                        <label class="control-label" for="สำกัดกอง">สำกัดกอง</label>
                        <div class="controls ">
                            <select id="lvl1_id" name="lvl1_id" style="width: 350px;">

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT * "
                                        . " FROM v_lvl1 ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->lvl1_id == $lvl1_id)
                                        $selected = "selected";
                                    echo "<option value='$rows->lvl1_id' $selected>$rows->lvl1_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>

                    <div class='control-group'>
                        <label class="control-label" for="แผนงาน">แผนงาน</label>
                        <div class="controls ">
                            <select id="panngan2_id" name="panngan2_id" class="span5" >

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT DISTINCT SUBSTR(lvl_id,1,2) AS lvl_id, panngan2_id, panngan2_name "
                                        . " FROM v_panngan2 "
                                        . " ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->panngan2_id == $panngan2_id)
                                        $selected = "selected";
                                    echo "<option class='$rows->lvl_id' value='$rows->panngan2_id' $selected>$rows->panngan2_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                    <div class='control-group'>
                        <label class="control-label" for="งาน">งาน</label>
                        <div class="controls ">
                            <select id="panngan3_id" name="panngan3_id" style="width: 350px;">

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT * "
                                        . " FROM v_panngan3 ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->panngan3_id == $panngan3_id)
                                        $selected = "selected";
                                    echo "<option value='$rows->panngan3_id' class='$rows->panngan2_id' $selected>$rows->panngan3_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                    <div class='control-group'>
                        <label class="control-label" for="หมวด">หมวด</label>
                        <div class="controls ">
                            <select id="money_out2_id" name="money_out2_id" style="width: 550px;">

                                <option value="">--</option>
                                <?php
                                $sql = "SELECT * "
                                        . " FROM money_out2 ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->money_out2_id == $money_out2_id)
                                        $selected = "selected";

                                    echo "<option value='$rows->money_out2_id' $selected>$rows->money_out2_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>

                    <div class='control-group'>
                        <label class="control-label" for="ประเภท">ประเภท</label>
                        <div class="controls ">
                            <select id="money_out3_id" name="money_out3_id" style="width: 450px;">
                                <option value="">--</option>
                                <?php
                                $sql = "SELECT * "
                                        . " FROM money_out3 ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->money_out3_id == $money_out3_id)
                                        $selected = "selected";

                                    echo "<option value='$rows->money_out3_id' class='$rows->money_out2_id' $selected>$rows->money_out3_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                    <div class='control-group'>
                        <label class="control-label" for="ผู้รับเงิน">ผู้รับเงิน</label>
                        <div class="controls ">
                            <select id="vender_id" name="vender_id" style="width: 250px;">
                                <option value="">--</option>
                                <?php
                                $sql = "SELECT * "
                                        . " FROM v_vender ";

                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    if ($rows->vender_id == $vender_id)
                                        $selected = "selected";

                                    echo "<option value='$rows->vender_id' $selected>$rows->vender_name</option>";
                                    $selected = "";
                                }
                                ?>

                            </select>
                        </div>
                    </div>

                    <div class='control-group'>
                        <label class="control-label" for="ประจำเดือน">ประจำเดือน</label>
                        <div class="controls ">
                            <input type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" />

                        </div>
                    </div>

                    <div class='control-group'>
                        <label class="control-label" for="จำนวนเงิน">จำนวนเงิน</label>
                        <div class="controls ">
                            <input type="text" class="text-right" id="amount" name="amount" value="<?php echo $amount; ?>" />

                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="หมายเหตุ">หมายเหตุ</label>
                        <div class="controls">
                            <textarea id="comments" name="comments" rows="7" style="width: 350px" placeholder="....."><?php
                                if ($comments) {
                                    echo $comments;
                                } else {
                                    //echo "เบิกจ่ายให้แก่... \nเพื่อ.....";
                                }
                                ?></textarea>

                        </div>
                    </div>
                </div>
                <p class="text-center">
                    <button type="submit" id="submit" name="submit"  class="btn btn-primary"><i class="icon-ok icon-white"></i> บันทึกข้อมูล</button>
                    <a href="index.php?pages=petout&resource=OUT" class="btn close-dialog"><i class="icon-remove"></i> ยกเลิก</a>
                </p>
            </div>
        </div>
    </div>

</form>

